﻿using Cysharp.Threading.Tasks;
using Data;
using Hero;
using System;
using UnityEngine;

namespace Level.Enemies
{
    public class BombDeath : MonoBehaviour
    {
        [SerializeField] private Bomb _bomb;
        [SerializeField] private BombHealth _health;
        [SerializeField] private BombMove _move;
        private GameSceneData _data;
        private HeroScore _heroScore;

        public void Construct(GameSceneData data, HeroScore heroScore)
        {
            _data = data;
            _heroScore = heroScore;
        }

        private void Start() =>
            _health.Destroyed += OnBombDestroy;

        private void OnDestroy() =>
            _health.Destroyed -= OnBombDestroy;

        private async void OnBombDestroy()
        {
            _move.Stop();
            await UniTask.Delay(TimeSpan.FromSeconds(_data.CreateAnimationTime));
            if (!_bomb) 
                return;
            _data.LootPieces.Remove(_bomb.gameObject);
            _heroScore.SetScore(_bomb.Value);
            Destroy(gameObject);
        }
    }
}